<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item :label="$t('parameter.settingsTask.taskName')" prop="taskName">
        <el-input v-model="queryParams.taskName" :placeholder="$t('parameter.settingsTask.placeholder.taskName')"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item :label="$t('parameter.settingsTask.deviceTypeName')" prop="deviceTypeId">
        <el-select @change="deviceTypeChange" v-model="queryParams.deviceTypeId" clearable size="middle" style="width: 240px">
          <el-option v-for="dict in deviceTypeOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('parameter.settingsTask.deviceModelName')" prop="deviceModelId">
        <el-select v-model="queryParams.deviceModelId" clearable size="middle" style="width: 240px">
          <el-option v-for="dict in deviceModelOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('common.button.query')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('common.button.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['parameter:settingsTask:export']">
          {{$t('common.button.export')}}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commandTaskList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('parameter.settingsTask.taskName')" align="center" prop="taskName" :show-overflow-tooltip="true" width="120"/>
      <el-table-column :label="$t('parameter.settingsTask.deviceTypeName')" align="center" prop="deviceTypeName" :show-overflow-tooltip="true" width="120"/>
      <el-table-column :label="$t('parameter.settingsTask.deviceModelName')" align="center" prop="deviceModelName" :show-overflow-tooltip="true" width="120"/>
      <el-table-column :label="$t('parameter.settingsTask.issueTotal')" align="center" prop="issueTotal" :show-overflow-tooltip="true" width="120"/>
      <el-table-column :label="$t('parameter.settingsTask.issueWaitTotal')" align="center" prop="issueWaitTotal" :show-overflow-tooltip="true" width="120" />
      <el-table-column :label="$t('parameter.settingsTask.issuingTotal')" align="center" prop="issuingTotal" :show-overflow-tooltip="true" width="120" />
      <el-table-column :label="$t('parameter.settingsTask.issueSuccessTotal')" align="center" prop="issueSuccessTotal" :show-overflow-tooltip="true" width="120"/>
      <el-table-column :label="$t('parameter.settingsTask.issueFailTotal')" align="center" prop="issueFailTotal" :show-overflow-tooltip="true" width="120" />
      <el-table-column :label="$t('parameter.settingsTask.issueTerminateTotal')" align="center" prop="issueTerminateTotal" :show-overflow-tooltip="true" width="120"/>
      <el-table-column :label="$t('parameter.settingsTask.isClose')" align="center" prop="isClose" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isClose" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('parameter.settingsTask.remark')" align="center" prop="remark" :show-overflow-tooltip="true"  width="220"/>
      <el-table-column  :label="$t('common.table.operate')" width="220" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view"
            @click="viewDetails(scope.row)" v-hasPermi="['parameter:settingsTask:detail']">{{$t('parameter.settingsTask.viewDetails')}}</el-button>
          <el-button size="mini" type="text" icon="el-icon-close"
                     :disabled="scope.row.isClose !== 'N'"
                     @click="cancelIssue(scope.row.taskId)" v-hasPermi="['parameter:settingsTask:cancel']">{{$t('parameter.settingsTask.cancelIssue')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <Details ref="Details"></Details>

  </div>
</template>

<script>
import { listCommandTask, cancelTask } from "@/api/parameter/settingsTask";
import { typeTree } from "@/api/device/type";
import { modelTree } from "@/api/device/model";
import Details from "./modules/Details";

export default {
  name: "CommandTask",
  dicts: ['sys_yes_no'],
  components: {Details},
  data() {
    return {
      deviceTypeOptions: [],
      deviceModelOptions: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数下发指令任务设备表格数据
      commandTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: undefined,
        deviceTypeId: undefined,
        deviceModelId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [
          { required: true, message: this.$t('parameter.settingsTask.rules.taskName'), trigger: "blur" }
        ],
        deviceTypeId: [
          { required: true, message: this.$t('parameter.settingsTask.rules.deviceTypeId'), trigger: "blur" }
        ],
        deviceModelId: [
          { required: true, message: this.$t('parameter.settingsTask.rules.deviceModelId'), trigger: "blur" }
        ],
        remark: [
          { required: true, message: this.$t('parameter.settingsTask.rules.remark'), trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTypeTree();
  },
  methods: {
    deviceTypeChange(typeId) {
      if (typeId) {
        this.getModelTree(typeId)
      } else {
        this.form.deviceModelId = undefined
        this.deviceModelOptions = []
      }
    },
    getTypeTree() {
      typeTree().then(response => {
        this.deviceTypeOptions = response.data;
      });
    },
    getModelTree(typeId) {
      modelTree(typeId).then(response => {
        this.deviceModelOptions = response.data;
      });
    },
    /** 查询参数下发指令任务设备列表 */
    getList() {
      this.loading = true;
      listCommandTask(this.queryParams).then(response => {
        this.commandTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/command/task/export', {
        ...this.queryParams
      }, `commandTask_${new Date().getTime()}.xlsx`)
    },
    viewDetails(row) {
      this.$refs['Details'].init(row.taskId);
    },
    cancelIssue(taskId) {
      const message = this.$t('parameter.settingsTask.cancelIssueConfirm');
      const title = this.$t('common.prompt.tip');
      this.$confirm(message, title, {
        confirmButtonText: this.$t('common.button.confirm'),
        cancelButtonText: this.$t('common.button.cancel'),
        type: "warning",
      }).then(function () {
        return cancelTask(taskId)
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('parameter.settingsTask.cancelIssueSuccess'));
      })
    },
  }
};
</script>
